package com.game.server.util; 

import com.game.majiang.config.LogManager;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author 
 *
 */
public class Util {
	/**
	 * @param plainText
	 */
	static public String Md5(String plainText ) { 
		StringBuffer buf  = null;
		try { 
			MessageDigest md = MessageDigest.getInstance("MD5"); 
			md.update(plainText.getBytes()); 
			byte b[] = md.digest(); 
			int i; 
			buf = new StringBuffer(""); 
			for (int offset = 0; offset < b.length; offset++) { 
				i = b[offset]; 
				if(i<0) i+= 256; 
				if(i<16) 
					buf.append("0"); 
				buf.append(Integer.toHexString(i)); 
			} 
		} catch (NoSuchAlgorithmException e) {  
			e.printStackTrace(); 
		} 
		return buf.toString();
	}

	public static long dateToStamp(String str) {

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = simpleDateFormat.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long ts = date.getTime();
		return ts;
	}

    public static boolean useSetArr(String[] searchs, String searchVal) {
		Set<String> set = new HashSet<String>(Arrays.asList(searchs));
		return set.contains(searchVal);
    }

    public void test()
	{
		LogManager.getInstance().Log(Util.StringReplace("ÓÎ¿Í10001"));
	}

	public static boolean isChineseChar(String str){
		boolean temp = false;
		Pattern p=Pattern.compile("[\u4e00-\u9fa5]{2,4}"); 
		Matcher m=p.matcher(str); 
		if(m.matches()){ 
			temp =  true;
		}
		return temp;
	}

	/**
	 * ÅÐ¶Ï×Ö·û´®ÊÇ·ñÊÇÕûÊý
	 */
	public static boolean isInteger(String value) {
		try {
			Integer.parseInt(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	public static boolean isEngAndNumber(String str){
		boolean temp = false;
		Pattern p=Pattern.compile("^\\w+"); 
		Matcher m=p.matcher(str); 
		if(m.matches()){ 
			temp =  true;
		}
		return temp;
	}

	public static boolean checkPhone(String str){
		boolean temp = false;
		Pattern p=Pattern.compile("^1\\d{10}$"); 
		Matcher m=p.matcher(str); 
		if(m.matches()){ 
			temp =  true;
		}
		return temp;
	}

	public static String StringReplace(String str)
	{
		if(str.length() < 3)
		{
			return str + "***";
		}
		Pattern p = Pattern.compile("(\\S{2})(\\S+)(\\S{1})");  
		Matcher m = p.matcher(str);  
		str = m.replaceAll("$1***$3");
		return str;
	}

	public static String StringFilter(String str) {  
		// Ö»ÔÊÐí×ÖÄ¸ºÍÊý×ÖºÍÖÐÎÄ//[\\pP¡®¡¯¡°¡±  
		String regEx = "^[A-Za-z\\d\\u4E00-\\u9FA5\\p{P}¡®¡¯¡°¡±]+";  
		Pattern p = Pattern.compile(regEx);  
		StringBuilder sb = new StringBuilder(str);  
		for (int len = str.length(), i = len - 1; i >= 0; --i) {  
			if (!p.matches(regEx, String.valueOf(str.charAt(i)))) {  
				sb.deleteCharAt(i);  
			}  
		}  
		return sb.toString();  
	}  
	/**
	 * 判断两个日期否是同一周
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameDate(Date olddate, Date newdate) 
	{
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		String date1 = f.format(olddate);
		String date2 = f.format(newdate);

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date d1 = null;
		Date d2 = null;
		try 
		{
			d1 = format.parse(date1);
			d2 = format.parse(date2);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setFirstDayOfWeek(Calendar.MONDAY);//西方周日为一周的第一天，咱得将周一设为一周第一天
		cal2.setFirstDayOfWeek(Calendar.MONDAY);
		cal1.setTime(d1);
		cal2.setTime(d2);
		int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
		if (subYear == 0)// subYear==0,说明是同一年
		{
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		}
		else if (subYear == 1 && cal2.get(Calendar.MONTH) == 11) //subYear==1,说明cal比cal2大一年;java的一月用"0"标识，那么12月用"11"
		{
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		}
		else if (subYear == -1 && cal1.get(Calendar.MONTH) == 11)//subYear==-1,说明cal比cal2小一年
		{
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		}
		return false;
	}

	//	 public static void main(String[] args) 
	//	 {
	//	  boolean a = isSameDate("2016-12-25", "2017-1-1");
	//	  if (a) {
	//	   LogManager.getInstance().Log("是同一周！");
	//	  } else {
	//	   LogManager.getInstance().Log("不是同一周！");
	//	  }
	//
	//	  
	//	 
	//	 }
	/**
	 * 是否同一天
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isTheSameDay(Date date1, Date date2) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);

		boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
				.get(Calendar.YEAR);
		boolean isSameMonth = isSameYear
				&& cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
		boolean isSameDate = isSameMonth
				&& cal1.get(Calendar.DAY_OF_MONTH) == cal2
				.get(Calendar.DAY_OF_MONTH);

		return isSameDate;
	}
	/** 
	 * 判断字符串是否全部为中文字符组成 
	 * @param str   检测的文字 
	 * @return  true：为中文字符串，false:含有非中文字符 
	 */  
	public static boolean isChineseStr(String str){  
		Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]");  
		char c[] = str.toCharArray();  
		for(int i=0;i<c.length;i++){  
			Matcher matcher = pattern.matcher(String.valueOf(c[i]));  
			if(!matcher.matches()){  
				return false;  
			}  
		}  
		return true;  
	}  
	/** 
	 * 判断字符串是否含有中文
	 * @param str   检测的文字 
	 * @return  true：有，false:没有
	 */  
	public static boolean judgeChineseStr(String str){  
		Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]");  
		char c[] = str.toCharArray();  
		for(int i=0;i<c.length;i++){  
			Matcher matcher = pattern.matcher(String.valueOf(c[i]));  
			if(matcher.matches()){  
				return true;  
			}  
		}  
		return false;  
	}  
	/**
	 * 判断英文名
	 */
	public static boolean judgeEnglish(String str){
		//		String str = " Gre ll";
		str = str.trim();
		//		str =str.replace("(^\\s*)|(\\s*$)", "");
		Pattern pattern = Pattern.compile("^[A-Za-z][A-Za-z\\s]*[A-Za-z]$");  
		Matcher m=pattern.matcher(str); 
		if(m.matches())
		{ 
			return true;  
		}
		else
		{
			return false;  
		}
	}
	/**
	 * 判断微信号
	 */
	public static boolean judgeWechatNumber(String str){

		Pattern pattern = Pattern.compile("^[a-zA-Z]{1}[-_a-zA-Z0-9]{5,19}$");  
		Matcher m=pattern.matcher(str); 
		if(m.matches())
		{ 
			return true;  
		}
		else
		{
			return false;  
		}
	}
	/**
	 * 屏蔽特殊符号
	 * @param source
	 * @return
	 */
	public static String chuliWeChat2(String source) { 
		if(source != null)
		{
			//        	 String regEx = "//[^\x{4e00}-/\x{9fa5}0-9a-zA-Z]//iu";  
			//        	 String regEx = "//[^\x{4e00}-/\x{9fa5}0-9a-zA-Z]//iu";  
			//        	 String regEx="^[a-zA-Z0-9\\u4e00-\\u9fa5\\,，]+$"; //判断是汉字 字母 数字
			String regEx="([-+*/^()\\]\\[\"|/\\\\<>?？&$￥%!！。，,.#@;:：；“”‘’🤔\t\n])"; 
			//        	 String regEx="^[a-zA-Z0-9\\u4e00-\\u9fa5\\,ï¼]+$"; //å¤æ­æ¯æ±å­ å­æ¯ æ°å­
			//        	String regEx="([-+*/^()\\]\\[\"|/\\\\<>?ï¼&$ï¿¥%!ï¼ãï¼,.#@;:ï¼ï¼ââââ\t\n])"; 
			Pattern emoji = Pattern.compile (regEx ) ;
			Matcher emojiMatcher = emoji.matcher(source);
			if ( emojiMatcher.find())
			{
				source = emojiMatcher.replaceAll("口");
				return source;
			}         
			return source;
		}
		return source; 
	}
	/**
	 * 过滤表情<br>
	 * @param source
	 * @return
	 */
	public static String filterEmoji(String source) { 
		if(source != null)
		{
			Pattern emoji = Pattern.compile ("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]|[\ue000-\uf8ff]",Pattern.UNICODE_CASE | Pattern . CASE_INSENSITIVE ) ;
			Matcher emojiMatcher = emoji.matcher(source);
			if ( emojiMatcher.find())
			{
				source = emojiMatcher.replaceAll("口");
				return source;
			}
			return source;
		}
		return source; 
	}
	//所有小写
	public static boolean checkXiaoXie(String source) {

		if(source != null)
		{
			String regEx="([-+*/^()\\]\\[\"|/\\\\<>?？&$￥%!！。，,.#@;:：；“”‘’🤔\t\n])"; 
			Pattern emoji = Pattern.compile (regEx ) ;
			Matcher emojiMatcher = emoji.matcher(source);
			if ( emojiMatcher.find())
			{
				return false;
			}         
			regEx="([a-z])"; 
			emoji = Pattern.compile (regEx ) ;
			emojiMatcher = emoji.matcher(source);
			if ( emojiMatcher.find())
			{               
				return false;
			}         
			return true;
		}
		return false; 

	}
	//字母小写
	public static boolean checkisXiao(String source) {
		if(source != null)
		{
			String regEx="([a-z])"; 
			Pattern emoji = Pattern.compile (regEx ) ;
			Matcher emojiMatcher = emoji.matcher(source);
			if ( emojiMatcher.find())
			{
				return true;
			}         

			return false;
		}
		return false; 


	}

	public static ArrayList<Integer> dividendPack(int money,int count, int type) {


		//			int bili = Integer.parseInt(DataManager.getInstance().systemValue("hongbaominimum"));
		//			int ccc = money*bili/100;
		int ccc = 1;
		if (type == 0) {
			ccc = money/count;
		}
		
		int[] arrHongbao = new int[count];
		for (int i = 0; i < arrHongbao.length; i++) {
			int mm = ccc;
			if (money < ccc) {
				mm = money;
			}
			arrHongbao[i] = mm;
			money-=mm;
			if (money == 0) {
				break;
			}
		}

		if (money > 0) {
			int number = count;
			for (int i = 0; i < count; i++) {
				int max = money/number*2;
				Random r = new Random();
				int m = money;
				if (max > 0) {
					m = r.nextInt(max);
				} else {
					m = r.nextInt(money);
				}
				 
				if (m > money) {
					m = money;
				}
				arrHongbao[i] = arrHongbao[i]+m;
				money-=m;
				number--;
				if (money == 0) {
					break;
				}
			}
			if (money > 0) {
				arrHongbao[count - 1] = arrHongbao[count - 1]+money;
			}

		}
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		for (int i = 0; i < arrHongbao.length; i++) { 						
			list.add(arrHongbao[i]);
		}
		
		return list;
	}
	/**
	 * [min, max)
	 * @param min
	 * @param max
	 * @return
	 */
	public static int random(int min, int max) {
		return min + random(max - min);
	}
	public static int random(int max) {
		if(max <= 0) {
			return 0;
		}
		Random r = new Random();
		return r.nextInt(max);
	}

	public static char getRandomHan() {
        return (char)(0x4e00 + random(0x9fa5 - 0x4e00 + 1)); 
    }

}